package kr.hkit.summary_java.erp.UI;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.awt.GridLayout;

import kr.hkit.myframework.uicomp.ComboPanel;
import kr.hkit.myframework.uicomp.InputPanel;
import kr.hkit.summary_java.erp.dao.DaoInterface;
import kr.hkit.summary_java.erp.dao.EmployeeDAO;
import kr.hkit.summary_java.erp.dto.Employee;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.sql.SQLException;

public class PanelEmp extends JPanel implements ActionListener {
	private InputPanel panel_empno;
	private InputPanel panel_empname;
	private ComboPanel panel_title;
	private ComboPanel panel_manager;
	private InputPanel panel_salary;
	private ComboPanel panel_deptno;
	private JButton btnOk;
	private JPanel panel_btn;
	private Employee d;

	private EmployeeDAO dao;
	/**
	 * Create the panel.
	 */
	public PanelEmp(EmployeeDAO dao) {
		this.dao = dao;
		initialize();
	}
	private void initialize() {
		setLayout(new GridLayout(0, 1, 0, 0));
				
		panel_empno = new InputPanel("사번");
		add(panel_empno);
		panel_empno.getTf().addFocusListener(new FocusAdapter() {

			@Override
			public void focusLost(FocusEvent e) {
				super.focusLost(e);
				int no = Integer
						.parseInt(panel_empno.getTf().getText().trim());
				d = new Employee();
				d.setEmpNo(no);
				try {
					d = dao.searchItem(d);
					System.out.println(d);
					panel_empname.setTf(d.getEmpName());
					panel_salary.setTf(d.getSalary()+"");
					if (!btnOk.getText().equals("검색")) {
						panelEnable();
					}
				} catch (SQLException e1) {
					e1.printStackTrace();
				} catch (NullPointerException e1) {
					if (!btnOk.getText().equals("추가"))
						JOptionPane.showMessageDialog(PanelEmp.this,
								"해당 사원이 없습니다.");
					else
						JOptionPane.showMessageDialog(PanelEmp.this, "추가 가능");
				}
			}

		});
		
		panel_empname = new InputPanel("사원명");
		add(panel_empname);
		
		panel_title = new ComboPanel("직책");
		add(panel_title);
		
		panel_manager = new ComboPanel("매니저");
		add(panel_manager);
		
		panel_salary = new InputPanel("급여");
		add(panel_salary);
		
		panel_deptno = new ComboPanel("부서");
		add(panel_deptno);
		
		panel_btn = new JPanel();
		add(panel_btn);
		
		btnOk = new JButton();
		btnOk.addActionListener(this);
		panel_btn.add(btnOk);
	}
	public InputPanel getPanel_empno() {
		return panel_empno;
	}
	public void setPanel_empno(InputPanel panel_empno) {
		this.panel_empno = panel_empno;
	}
	public InputPanel getPanel_empname() {
		return panel_empname;
	}
	public void setPanel_empname(InputPanel panel_empname) {
		this.panel_empname = panel_empname;
	}
	public ComboPanel getPanel_title() {
		return panel_title;
	}
	public void setPanel_title(ComboPanel panel_title) {
		this.panel_title = panel_title;
	}
	public ComboPanel getPanel_manager() {
		return panel_manager;
	}
	public void setPanel_manager(ComboPanel panel_manager) {
		this.panel_manager = panel_manager;
	}
	public InputPanel getPanel_salary() {
		return panel_salary;
	}
	public void setPanel_salary(InputPanel panel_salary) {
		this.panel_salary = panel_salary;
	}
	public ComboPanel getPanel_deptno() {
		return panel_deptno;
	}
	public void setPanel_deptno(ComboPanel panel_deptno) {
		this.panel_deptno = panel_deptno;
	}
	public JButton getBtnOk() {
		return btnOk;
	}
	public void setBtnOk(String btnOk) {
		this.btnOk.setText(btnOk);
	}

	private Employee getDept() {
		int eno = Integer.parseInt(panel_empno.getTf().getText().trim());
		String name = panel_empname.getTf().getText();
		return new Employee(eno, name);
	}

	private void panelDisable() {
		panel_empno.getTf().setEnabled(true);
		panel_empname.getTf().setEnabled(false);
		panel_salary.getTf().setEnabled(false);
		panel_title.setEnabled(false);
		panel_manager.setEnabled(false);
		panel_deptno.setEnabled(false);
	}

	private void panelEnable() {
		panel_empno.getTf().setEnabled(false);
		panel_empname.getTf().setEnabled(true);
		panel_salary.getTf().setEnabled(true);
		panel_title.setEnabled(true);
		panel_manager.setEnabled(true);
		panel_deptno.setEnabled(true);
	}

	private void panelClear() {
		panel_empno.setTf("");
		panel_empname.setTf("");
		panel_salary.setTf("");
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("추가")) {
			actionPerformedBtnAdd(e);
		} else if(e.getActionCommand().equals("삭제")){
			actionPerformedBtnDel(e);
		} else if(e.getActionCommand().equals("수정")){
			actionPerformedBtnUpdate(e);
		} else if(e.getActionCommand().equals("검색")){
			actionPerformedBtnSearch(e);
		} else {//리스트보기
			actionPerformedBtnList(e);
		}
	}
	private void actionPerformedBtnList(ActionEvent e) {
	}
	private void actionPerformedBtnSearch(ActionEvent e) {		
	}
	private void actionPerformedBtnUpdate(ActionEvent e) {		
	}
	private void actionPerformedBtnDel(ActionEvent e) {
	}
	private void actionPerformedBtnAdd(ActionEvent e) {
	}
}
